/*
 * Copyright (C) 2011 TargetBoard
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * @author amc
 *
 */
public class XmlExtractor<T> {
	private static final String JAXB_CLASSES = "jaxb.generated.datatable:jaxb.generated.users:jaxb.generated.book";
	private String path;

	public XmlExtractor(String path) {
		this.path = path;
	}

	private Unmarshaller getUnmarshaller() throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(JAXB_CLASSES);
		return jc.createUnmarshaller();
	}

	public T extract() {
		T data = null;
		try {
			Unmarshaller u = getUnmarshaller();
			JAXBElement<T> element = (JAXBElement<T>) u.unmarshal(new File(path));
			data = element.getValue();
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}
}
